import pos, { PosAppPrintTemplate, PosGetIn } from 'api/pos';
import { RadiusButton, Toast } from 'components';
import React, { useEffect, useState, Fragment } from 'react';
import { Image, ScrollView, Text, TouchableOpacity, View } from 'react-native';
import { NavigationStackScreenComponent } from 'react-navigation-stack';
import { goBack } from 'services/navigation';
import { commonStyle, translateSize } from 'styles';
import { WebView } from '../../native-modules/react-native-webview';


import assets from './assets';
import { changeStyle } from './styles';
import PrintModule from 'native-modules/PrintModule';
import Loading from 'components/loading';
import { operate } from 'services/authorization';
import { getAuthorization } from '../services/authorization';

const ShowBillScreen: NavigationStackScreenComponent<{ billUrl: string, voucherId: number, templateId?: number, remarks?: string, getInFeeIdList?: number[], load: () => void }> = props => {

	useEffect(() => {
		Loading.show()
		setTimeout(() => { Loading.hide() }, 2000)
	}, [])
	let voucherLock = false
	return (
		<View style={[commonStyle.containerColumn, changeStyle.container]}>
			<WebView bounces={false}
				scalesPageToFit={true}
				source={{ uri: props.navigation.getParam('billUrl'), method: 'GET' }}
				style={{ width: '100%', height: '100%' }}>
			</WebView>
			{props.navigation.getParam('templateId') ? <RadiusButton style={changeStyle.comfirmBtn} onPress={() => {
				if (voucherLock) return
				console.log('1111222')
				voucherLock = true
				Loading.show()
				pos.posApp.generateBill({ getInFeeIdList: props.navigation.getParam('getInFeeIdList'), remarks: props.navigation.getParam('remarks'), templateId: props.navigation.getParam('templateId'), projectId: getAuthorization().project.id, preview: 0 }).then(
					res => {
						if (res.result) {
							Toast.show('生成成功')
							Loading.hide()
							if (res.data.electronicBillUrl !== '') {
								props.navigation.replace('ShowBill', {
									billUrl: res.data.electronicBillUrl,
									voucherId: res.data.id,
								})
							}
						}
						else {
							Toast.show('生成失败!' + res.msg)
							Loading.hide()
						}
					}
				).catch(err => {
					Loading.hide()
				}).finally(() => {
					props.navigation.getParam('load')()
					voucherLock = false
				})
			}}>
				<Text style={changeStyle.btnText}>生成电子票据</Text>
			</RadiusButton> : (props.navigation.getParam('templateId') ? <RadiusButton style={changeStyle.comfirmBtn} onPress={() => {
				pos.posApp.getBillImgByVoucherId({ id: props.navigation.getParam('voucherId') }).then(res => {
					if (res.result) {
						console.log('print', res);
						PrintModule.printImg(res.data).then(ress => {
							console.log('print', ress);
						}).catch(err => {
							console.log('print err', JSON.stringify(err));
						});
					}
				})
			}}>
				<Text style={changeStyle.btnText}>打印</Text>
			</RadiusButton> : <Fragment />)

			}
		</View>
	)
}

ShowBillScreen.navigationOptions = props => {
	return {
		title: props.navigation.getParam('templateId') ? '预览电子票据' : '查看电子票据',
		// headerRight: (props.navigation.getParam('templateId') ? <TouchableOpacity
		// 	style={commonStyle.headerRight}
		// 	onPress={() => {
		// 		Loading.show()
		// 		pos.posApp.generateBill({ getInFeeIdList: props.navigation.getParam('getInFeeIdList'), templateId: props.navigation.getParam('templateId'), projectId: getAuthorization().project.id, preview: 0 }).then(
		// 			res => {
		// 				if (res.result) {
		// 					Toast.show('生成成功')
		// 					Loading.hide()
		// 					if (res.data.electronicBillUrl !== '') {
		// 						props.navigation.replace('ShowBill', {
		// 							billUrl: res.data.electronicBillUrl,
		// 							voucherId: res.data.id,
		// 						})
		// 					}
		// 				}
		// 				else {
		// 					Toast.show('生成失败!' + res.msg)
		// 					Loading.hide()
		// 				}
		// 			}
		// 		).catch(err => {
		// 			Loading.hide()
		// 		}).finally(() => {
		// 			load()
		// 		})

		// 	}}>
		// 	<Text style={commonStyle.headerFont}>生成电子票据</Text>
		// </TouchableOpacity> : (props.navigation.getParam('voucherId') ?
		// 	<TouchableOpacity
		// 		style={commonStyle.headerRight}
		// 		onPress={() => {
		// 			operate()
		// 			pos.posApp.getBillImgByVoucherId({ id: props.navigation.getParam('voucherId') }).then(res => {
		// 				if (res.result) {
		// 					console.log('print', res);
		// 					PrintModule.printImg(res.data).then(ress => {
		// 						console.log('print', ress);
		// 					}).catch(err => {
		// 						console.log('print err', JSON.stringify(err));
		// 					});
		// 				}
		// 			})

		// 		}}>
		// 		<Text style={commonStyle.headerFont}>打印</Text>
		// 	</TouchableOpacity> : null)
		// ),
	}
}

export default ShowBillScreen
